package cn.maihe.elg.operation.utils;


import cn.hutool.core.collection.ConcurrentHashSet;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2018/5/16
 *
 * @author by JHWei
 */
public class IdGenerate {

    /**
     * 主机和进程的机器码
     */
    private static final Sequence worker = new Sequence();

    final static int MAX = 10000;

    final static int length = String.valueOf(MAX).length();

    static AtomicInteger adder = new AtomicInteger();

    public static long getId() {
        return worker.nextId();
    }

    public static String getIdStr() {
        return String.valueOf(worker.nextId());
    }

    /**
     * @param len 保留位数
     * @return
     */
    public static String getIdStr(int len) {
        long tmpId = getId();
        if (String.valueOf(tmpId).length() < len) return String.format("%0" + len + "d", tmpId);
        return String.valueOf(tmpId).substring(String.valueOf(tmpId).length() - len);
    }

    public static long getId(int len) {
        return Long.valueOf(getIdStr(len));
    }

    /**
     * <p>
     * 获取去掉"-" UUID
     * </p>
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String next() {
        StringBuffer stringBuffer = new StringBuffer();
        long value = adder.incrementAndGet();
        if (value > MAX) {
            adder.set(0);
        }
        String str = value + "";
        for (int i = 0; i < length - str.length(); i++) {
            stringBuffer.append("0");
        }
        stringBuffer.append(str);
        return System.currentTimeMillis() + "" + stringBuffer.toString();
    }

    /**
     * @param centerNoEnum 中心编码枚举
     * @return
     */
    public static String genAcceptOrderNo(CenterNoEnum centerNoEnum) {
        return new StringBuilder()
                //yyyyMMdd 8位
                .append(centerNoEnum.getCenterNo())
                .append(IdGenerate.getIdStr(19))
                .toString();
    }

    public static String genRefundOrderNo() {
        return new StringBuilder()
                //yyyyMMdd 8位
                .append("REFUND")
                .append(IdGenerate.getIdStr(19))
                .toString();
    }

    public static String genApplyOrderNo() {
        return new StringBuilder()
                //yyyyMMdd 8位
                .append("APPLY")
                .append(IdGenerate.getIdStr(19))
                .toString();
    }

//    public static String genGuaranteeNo() {
//        return "ZTDB" + genContractNo();
//    }

    /**
     * 原截取导致重复，20220609调整修改为ZTDB+19位
     *
     * @return
     */
    public static String genGuaranteeNo() {
        return "DXDB" + IdGenerate.getIdStr();
    }

    /**
     * 原截取导致重复，20220609调整修改为ZTDB+19位
     *
     * @return
     */
    public static String genGuaranteeNo(String orgCode) {
        return orgCode + IdGenerate.getIdStr();
    }

    public static String genTranceId() {
        return new StringBuilder()
                //yyyyMMdd 8位
//                .append(DateUtil.getCurrentDate(DateUtil.FORMAT_TRADEDATE))
                .append("ELG")
                .append(IdGenerate.getIdStr(19))
                .toString();
    }

    /**
     * 合同编号：前缀2位
     */
    public static String genContractNo() {
        return IdGenerate.getIdStr();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            System.out.println("IdGenerate.getIdStr().substring(3) = " + IdGenerate.getIdStr().substring(3));
            System.out.println("IdGenerate.getIdStr().substring(3) = " + IdGenerate.getIdStr(20));
            System.out.println("IdGenerate.getIdStr().substring(3) = " + IdGenerate.getId(20));
        }
        ConcurrentHashSet<String> set = new ConcurrentHashSet<>(10000);

        for (int i = 0; i < 10000; i++) {
            CompletableFuture.runAsync(() -> {
                set.add(IdGenerate.getIdStr(10));
            });
        }
        System.out.println("set = " + set.size());


    }
}
